﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DoNet.Common.Utilities;
using Shop.Core;

namespace Shop.WebApp
{
    public partial class Login : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ViewState["RadomCode"] = RandomCode.GenerateRandomCode();
            }
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLongin_Click(object sender, EventArgs e)
        {
            try
            {
                string userCode = txtUserCode.Text.Trim();
                string password = txtPassWord.Text.Trim();
                string radomCode = txtRadom.Text.Trim();
                radomCode = radomCode.ToUpper();

                if (!radomCode.Equals(ViewState["RadomCode"]))
                    MessageBox.Show(this.Page, "随机码输入错误");
                else
                {
                    //验证
                    bool isValid = true;
                    if (!isValid)
                        MessageBox.Show(this.Page, "错误的用户名或者密码！");
                    else
                    {
                        //获得用户实体
                        //写seesion
                        PageHelper.SetSession(this.Page, "UserCode", "");
                        System.Web.Security.FormsAuthentication.RedirectFromLoginPage(userCode, false);
                    }
                }
            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }
        }
    }
}
